package main

import (
	"encoding/json"
	"log"
	"net/http"

	"golang.org/x/oauth2"
	"fmt"
	"git.oschina.net/YPTArch/basic/libs/remote"
	"net/url"
)

var (
	config1 = oauth2.Config{
		ClientID:     "dsfadafsdsfads1213fdsafdasfa",
		ClientSecret: "dfas135wef1fa1fe5wf1ewfa12fwef",
		Scopes:       []string{"all"},
		RedirectURL:  "http://192.168.1.100:9094/oauth2",
		Endpoint: oauth2.Endpoint{
			AuthURL:  "http://192.168.1.100:8080/usercenter/authorize",
			TokenURL: "http://192.168.1.100:8080/usercenter/token",
		},
	}
)




func main() {
	http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
		u := config1.AuthCodeURL("xyz")
		fmt.Println(u)
		http.Redirect(w, r, u, http.StatusFound)
	})
	http.HandleFunc("/codetoken", func(w http.ResponseWriter, r *http.Request) {
		u:=config1.Endpoint.AuthURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&response_type=token"+"&redirect_uri="+config1.RedirectURL
		fmt.Println(u)
		http.Redirect(w,r,u, http.StatusFound)
	})

	http.HandleFunc("/oauth2", func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		state := r.Form.Get("state")
		if state != "xyz" {
			http.Error(w, "State invalid", http.StatusBadRequest)
			return
		}
		code := r.Form.Get("code")
		fmt.Println(code)
		if code == "" {
			http.Error(w, "Code not found", http.StatusBadRequest)
			return
		}
		fmt.Println("--------------------------------------")

		u:=config1.Endpoint.TokenURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&grant_type=authorization_code"+"&redirect_uri="+config1.RedirectURL+"&code="+code+"&state="+ state
		fmt.Println(u)
		token, _, _, err := remote.HttpGet(u)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		e := json.NewEncoder(w)
		e.SetIndent("", "  ")
		e.Encode(&token)
	})

	http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
		u:=config1.Endpoint.TokenURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&grant_type=password"+"&redirect_uri="+config1.RedirectURL+"&username=13541085451&password=123456&scope=all"
		fmt.Println(u)
		token, _, _, err := remote.HttpGet(u)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		e := json.NewEncoder(w)
		e.SetIndent("", "  ")
		e.Encode(&token)
	})
	http.HandleFunc("/refreshtoken", func(w http.ResponseWriter, r *http.Request) {
		refreshToken:= r.URL.Query().Get("refresh_token")
		refreshToken=url.QueryEscape(refreshToken)
		u:=config1.Endpoint.TokenURL+"?client_id="+config1.ClientID+"&client_secret="+config1.ClientSecret+"&grant_type=refresh_token"+"&refresh_token="+refreshToken
		fmt.Println(u)
		token, _, _, err := remote.HttpGet(u)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		e := json.NewEncoder(w)
		e.SetIndent("", "  ")
		e.Encode(&token)
	})


	log.Println("Client is running at 9094 port.")
	log.Fatal(http.ListenAndServe(":9094", nil))
}
